package com.ssl.mall.product.controller;


import com.ssl.mall.common.bean.Result;
import com.ssl.mall.product.entity.Category;
import com.ssl.mall.product.entity.vo.CategoryVO;
import com.ssl.mall.product.service.ICategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 商品三级分类 前端控制器
 * </p>
 *
 * @author ssl
 * @since 2021-04-25
 */
@RestController
@RequestMapping("/product/category")
public class CategoryController {

    @Autowired
    private ICategoryService categoryService;

    @GetMapping("all/tree")
    public Result allWithTree() {
        List<CategoryVO> categories = categoryService.allWithTree();
        return Result.ok().put("data", categories);
    }

    /**
     * 保存
     *
     * @param category
     * @return
     */
    @PostMapping("save")
    public Result save(@RequestBody Category category) {
        categoryService.save(category);
        return Result.ok();
    }

    /**
     * 更新排序
     *
     * @param categories
     * @return
     */
    @PostMapping("update/sort")
    public Result updateSort(@RequestBody Category[] categories) {
        categoryService.updateBatchById(Arrays.asList(categories));
        return Result.ok();
    }

    /**
     * 级联更新
     *
     * @param category
     * @return
     */
    @PostMapping("update")
    public Result update(@RequestBody Category category) {
        categoryService.updateCascade(category);
        return Result.ok();
    }

    @GetMapping("info/{catId}")
    public Result info(@PathVariable("catId") Long catId) {
        return Result.ok().put("data", categoryService.getById(catId));
    }

    @PostMapping("delete")
    public Result delete(@RequestBody Long[] catIds){
        categoryService.removeCatByIds(Arrays.asList(catIds));
        return Result.ok();
    }
}

